local sk__suohun = fk.CreateSkill {

  name = "sk__suohun",

  tags = { Skill.Compulsory, },

}



sk__suohun:addEffect(fk.Damage, {
  name = "sk__suohun",
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if target and target == player and player:hasSkill(sk__suohun.name) then
      return data.from ~= data.to and not (data.from.dead or data.to.dead)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(data.to, "@sk__suohun", 1)
  end,
})
sk__suohun:addEffect(fk.Damaged, {
  name = "sk__suohun",
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if target and target == player and player:hasSkill(sk__suohun.name) then
      return data.from ~= data.to and not (data.from.dead or data.to.dead)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(data.from, "@sk__suohun", data.damage)
  end,
})
sk__suohun:addEffect(fk.EnterDying, {
  name = "sk__suohun",
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if target and target == player and player:hasSkill(sk__suohun.name) then
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:changeMaxHp(player, -((player.maxHp + 1) // 2))
    if not player.dead and player:isWounded() then
      room:recover({
        who = player,
        num = player:getLostHp(),
        recoverBy = player,
        skillName = sk__suohun.name,
      })
    end
    local targets = table.filter(room.alive_players, function(p)
      return p:getMark("@sk__suohun") > 0
    end)
    if #targets == 0 then return end
    room:doIndicate(player.id, table.map(targets, Util.IdMapper))
    for _, p in ipairs(targets) do
      if not p.dead then
        local n = p:getMark("@sk__suohun")
        room:setPlayerMark(p, "@sk__suohun", 0)
        room:damage {
          from = player,
          to = p,
          damage = n,
          skillName = sk__suohun.name,
        }
      end
    end
  end,
})


sk__suohun:addLoseEffect(function(self, player, is_death)
  local room = player.room
  if table.find(player.room:getOtherPlayers(player), function(p)
        return p:hasSkill(sk__suohun.name, true, true)
      end) then
    for _, p in ipairs(room:getAlivePlayers()) do
      room:setPlayerMark(p, "@sk__suohun", 0)
    end
  end
end)

sk__suohun:addEffect(fk.Deathed, {
  can_refresh = function(self, event, target, player, data)
    if target == player and not table.find(player.room:getOtherPlayers(player), function(p)
          return p:hasSkill(sk__suohun.name, true, true)
        end) then
      return player:hasSkill(sk__suohun.name, true, true)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room:getAlivePlayers()) do
      room:setPlayerMark(p, "@sk__suohun", 0)
    end
  end,
})

return sk__suohun
